import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/screenutil.dart';
import 'package:shop/config/config.dart';
import 'package:shop/http/HttpUtil.dart';
import 'package:shop/route/my_route.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:shared_preferences/shared_preferences.dart';
class OrderDetailPage extends StatefulWidget {
  String orId;//订单ID
  OrderDetailPage({Key key, this.title, this.orId}) : super(key: key);

  final String title;

  @override
  _OrderDetailPage createState() => _OrderDetailPage();
}

class _OrderDetailPage extends State<OrderDetailPage> {

  String orId;    //订单ID
  var order = {}; //订单信息
  String orStatus;//订单状态
  bool status=false;//是否找到订单信息

  //初始化数据
  @override
  void initState() {
    super.initState();
    this.orId=widget.orId;
    _getOrderInfo();
  }
  // _OrderDetailPage(orId){
  //   this.orId=orId;
  //   _getOrderInfo();
  // }
  //得订单详细信息
  _getOrderInfo() async {
    var res = await HttpUtil().get("/waimai/mOder/getMOrderById?orId="+orId);
    if(res['code']==200){
      order=res['data'];
      status=true;
      int orstatus = order['orStatus'];
      if(orstatus==0){orStatus="未支付";}else if(orstatus==1){orStatus=="已支付，待接单";}else if(orstatus==2){orStatus=="商家已接单";}else if(orstatus==3){orStatus=="骑手已接单，正赶往商家";}else
      if(orstatus==4){orStatus="已取货，配送中";}else if(orstatus==5){orStatus=="已完成";}else if(orstatus==6){orStatus=="待评价";}else if(orstatus==7){orStatus=="已评价";}
    }
    if(mounted){
      setState(() {

      });
    }

  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context , width: 464 , height: 820) ;
    return Scaffold(
      appBar: AppBar(
        title: Text("订单详情"),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [
            Container(
              height: 150,
              padding: EdgeInsets.symmetric(horizontal: 10),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Row(
                    children: [
                      Text("订单信息")
                    ],
                  ),
                  Row(
                    children: [
                      Container(child: Text("订  单  号:"),width: ScreenUtil().setWidth(80),),
                      SizedBox(width: 20,),
                      // Text(order['orOrdernumber'].toString())
                      Text(order['id'])
                    ],
                  ),
                  Row(
                    children: [
                      Container(child: Text("订单状态:"),width: ScreenUtil().setWidth(80),),
                      SizedBox(width: 20,),
                      Text("已完成")
                    ],
                  ),
                  Row(
                    children: [
                      Container(child: Text("下单时间:"),width: ScreenUtil().setWidth(80),),
                      SizedBox(width: 20,),
                      Text(order['orTime'].toString())
                    ],
                  ),
                  Row(
                    children: [
                      Container(child: Text("备        注:"),width: ScreenUtil().setWidth(80),),
                      SizedBox(width: 20,),
                      order['orRemark']==null ? Text("") : Text("上班时间不要打电话")
                    ],
                  ),
                  Divider(color: Colors.blueAccent),
                ],
              ),
            ),
            Container(
              height: 160,
              padding: EdgeInsets.symmetric(horizontal: 10),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [Row(
                  children: [Text("配送信息")],
                ),
                  Row(
                    children: [
                      Container(child: Text("收  货  人:"),width: ScreenUtil().setWidth(80),),
                      SizedBox(width: 20,),
                      Text(order['address']['adName'])
                    ],
                  ),
                  Row(
                    children: [
                      Container(child: Text("联系电话:"),width: ScreenUtil().setWidth(80),),
                      SizedBox(width: 20,),
                      Text(order['address']['adPhone'])
                    ],
                  ),
                  Row(
                    children: [
                      Container(child: Text("收货地址:"),width: ScreenUtil().setWidth(80),),
                      SizedBox(width: 20,),
                      // Expanded(child: Text("湖南省株洲市天元区珠江北路2000号嵩山街道幸福大厦909幸福大厦未来传媒")),
                      Expanded(child: Text(order['address']['addressDetail'])),
                    ],
                  ),
                  order['rider']==null || order['rider']=="" ? Row() : Row(
                    children: [
                      Container(child: Text("骑        手:"),width: ScreenUtil().setWidth(80),),
                      SizedBox(width: 20,),
                      Text(order['rider']['qsName']+","+order['rider']['phone'])
                    ],
                  ),
                  Divider(color: Colors.blueAccent),],
              ),
            ),
            Container(
              padding: EdgeInsets.symmetric(horizontal: 10),
              child: Column(
                children: [
                  Row(
                    children: [Text("商品信息")],
                  ),
                  Column(
                    children: [
                      _getOrderGoods(order['morderDetailList']),
                    ],
                  ),
                  Divider(color: Colors.blueAccent,),
                ],
              ),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text("共"+order['orTotalnum'].toString()+"件商品",style: TextStyle(fontSize: 18),),
                Row(
                  children: [Text("合计:"),Text("￥"+order['orPrice'].toString(),style: TextStyle(color: Colors.red,fontSize: 18),)],
                )
              ],
            )
          ],
        ),
      )
    );
  }

  //遍历订单详情商品
  _getOrderGoods(orderDetail){
    List<Widget> x = [];
    for(int i=0;i<orderDetail.length;i++){
      x.add(
        Container(
          // color: Colors.lightBlueAccent,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Image.network(Config.getInstance().getHost()+orderDetail[i]['mgoods']['gimages'],width: 90,height: 90,),
              SizedBox(width: 10,),
              Expanded(child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(orderDetail[i]['mgoods']['gname']),
                  SizedBox(height: 5,),
                  orderDetail[i]['guiges']!=null ? Text(orderDetail[i]['guiges'],style: TextStyle(color: Colors.black38),) : Text(""),
                  SizedBox(height: 5,),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: [
                      Text("￥"+orderDetail[i]['odCost'].toString(),style: TextStyle(color: Colors.lightBlue),),
                      Text("×"+orderDetail[i]['odNum'].toString(),style: TextStyle(color: Colors.lightBlue))
                    ],)
                ],
              ),),
            ],
          ),
        )
      );
    }
    return Container(
      child: Column(children: x),
    );
  }
}